/**
 * Multicast project Bidois Morgan & Thomas Nunes
 *
 */
package Engine;

import java.nio.ByteBuffer;

/**
 *
 * @author thomas
 *
 * The message object send enter entity
 */
public class Message {

    private byte[] content;
    private Header header;
    private int logicalClock;
    private int idClient;

    public Message(Header head, byte[] data) {
        this.logicalClock = head.getLogicalClock();
        this.content = data;
        this.idClient = head.getIdClient();
        this.header = head;
    }

    public Header getHeader() {
        return header;
    }

    public void setHeader(Header flag) {
        this.header = flag;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public int getIdClient() {
        return idClient;
    }

    public void setIdClient(int idClient) {
        this.idClient = idClient;
    }

    public int getLogicalClock() {
        return logicalClock;
    }

    public void setLogicalClock(int logicalClock) {
        this.logicalClock = logicalClock;
    }

    /**
     * get this message to Byte data lenght(4) - typeMessage(2) - LC(4) -
     * idClient(4) -Content (*)
     *
     * @return byte description of the Message
     */
    public ByteBuffer toByte() {
        ByteBuffer bufferOut;
        //allocate buffer with the size of data (n) + size of data (4) + flag type (2)
        bufferOut = ByteBuffer.allocate(this.content.length + 4 + 4 + 4 + 2);
        //NIO FRAME
        bufferOut.putInt(this.content.length);          //add the size
        bufferOut.putShort(this.header.getShort());    //add the flag (type of message)
        //MULTICAST FRAME
        bufferOut.putInt(this.logicalClock);
        bufferOut.putInt(this.idClient);
        bufferOut.put(this.content);                    //Add the the data
        return bufferOut;
    }
}
